package gee

import (
	"net/http/pprof"
)

// Wrap adds several routes from package `net/http/pprof` to *gee.engine object
func PprofWrapEngine(router *engine) {
	PprofWrapGroup(router.routerGroup)
}

// WrapGroup adds several routes from package `net/http/pprof` to *gee.routerGroup object
func PprofWrapGroup(router *routerGroup) {
	routers := []struct {
		Method  string
		Path    string
		Handler HandlerFunc
	}{
		{"GET", "/debug/pprofs", IndexHandler()},
		{"GET", "/debug/heap", HeapHandler()},
		{"GET", "/debug/pprof/heap", HeapHandler()},
		{"GET", "/debug/goroutine", GoroutineHandler()},
		{"GET", "/debug/pprof/goroutine", GoroutineHandler()},
		{"GET", "/debug/allocs", AllocsHandler()},
		{"GET", "/debug/pprof/allocs", AllocsHandler()},
		{"GET", "/debug/block", BlockHandler()},
		{"GET", "/debug/pprof/block", BlockHandler()},
		{"GET", "/debug/threadcreate", ThreadCreateHandler()},
		{"GET", "/debug/pprof/threadcreate", ThreadCreateHandler()},
		{"GET", "/debug/cmdline", CmdlineHandler()},
		{"GET", "/debug/pprof/cmdline", CmdlineHandler()},
		{"GET", "/debug/profile", ProfileHandler()},
		{"GET", "/debug/pprof/profile", ProfileHandler()},
		{"GET", "/debug/symbol", SymbolHandler()},
		{"GET", "/debug/pprof/symbol", SymbolHandler()},
		{"GET", "/debug/trace", TraceHandler()},
		{"GET", "/debug/pprof/trace", TraceHandler()},
		{"GET", "/debug/mutex", MutexHandler()},
		{"GET", "/debug/pprof/mutex", MutexHandler()},
	}

	for _, r := range routers {
		router.AddRouter(r.Method, r.Path, r.Handler)
	}
}

// IndexHandler will pass the call from /debug/pprof to pprof
func IndexHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Index(ctx.Resp, ctx.Req)
	}
}

// CmdlineHandler will pass the call from /debug/pprof/cmdline to pprof
func CmdlineHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Cmdline(ctx.Resp, ctx.Req)
	}
}

// ProfileHandler will pass the call from /debug/pprof/profile to pprof
func ProfileHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Profile(ctx.Resp, ctx.Req)
	}
}

// SymbolHandler will pass the call from /debug/pprof/symbol to pprof
func SymbolHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Symbol(ctx.Resp, ctx.Req)
	}
}

// TraceHandler will pass the call from /debug/pprof/trace to pprof
func TraceHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Trace(ctx.Resp, ctx.Req)
	}
}

// HeapHandler will pass the call from /debug/pprof/heap to pprof
func HeapHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Handler("heap").ServeHTTP(ctx.Resp, ctx.Req)
	}
}

// GoroutineHandler will pass the call from /debug/pprof/goroutine to pprof
func GoroutineHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Handler("goroutine").ServeHTTP(ctx.Resp, ctx.Req)
	}
}

// AllocsHandler will pass the call from /debug/pprof/allocs to pprof
func AllocsHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Handler("allocs").ServeHTTP(ctx.Resp, ctx.Req)
	}
}

// BlockHandler will pass the call from /debug/pprof/block to pprof
func BlockHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Handler("block").ServeHTTP(ctx.Resp, ctx.Req)
	}
}

// ThreadCreateHandler will pass the call from /debug/pprof/threadcreate to pprof
func ThreadCreateHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Handler("threadcreate").ServeHTTP(ctx.Resp, ctx.Req)
	}
}

// MutexHandler will pass the call from /debug/pprof/mutex to pprof
func MutexHandler() HandlerFunc {
	return func(ctx *Context) {
		pprof.Handler("mutex").ServeHTTP(ctx.Resp, ctx.Req)
	}
}
